package org.onion.pattern.event.springboot.rocketmq.consume;


import org.apache.commons.lang3.StringUtils;
import org.onion.pattern.event.springboot.rocketmq.annotation.RocketMQConsumerGroup;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.util.Assert;

public final class RocketMQConsumerGroupUtils {

    private RocketMQConsumerGroupUtils() {

    }

    public static String getGroupName(Class<?> targetClass) {
        RocketMQConsumerGroup consumerGroup = AnnotationUtils.findAnnotation(targetClass, RocketMQConsumerGroup.class);
        if (consumerGroup == null) {
            return "";
        }

        if (StringUtils.isNotBlank(consumerGroup.value())) {
            return checkGroupName(consumerGroup.value());
        }

        return checkGroupName(targetClass.getName().replace(".", "-").replace("$", "-"));
    }

    public static String checkGroupName(String groupName) {
        Assert.hasText(groupName, "组名不为空");
        if (StringUtils.contains(groupName, "%")) {
            throw new IllegalArgumentException("不能包含 %");
        }
        return groupName;
    }


}
